window.onload = function() {
	var userImage = document.getElementById("user_image");
	var user = document.getElementById("user");
	var backPage = document.getElementById("back_page");
	var navgation = document.getElementById("navgation");
	var user = document.getElementById("user");
	var userEmail = document.getElementById("user_email");
	var all = document.getElementById("all");

	var lineH = document.getElementById("bottom");
	var bottomP = lineH.getElementsByTagName("p");
	for(var i = 0; i < bottomP.length;i++){
		bottomP[i].style.lineHeight = getStyle(lineH,"height");
	}
	/*
	 * 动态设置导航栏宽度
	 * 以及用户页面的宽高
	 */
	//获取用户显示屏的宽高
	user.style.width = screen.width + "px";
	user.style.height = screen.height + "px";
	user.style.left = - screen.width + "px";
	/*
	 * 设置轮播图图片宽高
	 */
	var imgList = document.getElementById("imgList");
	var imgArr = imgList.getElementsByTagName("img");
	for(var i = 0;i < imgArr.length ; i++){
		imgArr[i].style.width = screen.width + "px";
	}
	imgList.style.left = -screen.width + "px";
	//动态设置图片列表的宽度
	imgList.style.width = screen.width*imgArr.length + "px";
	all.style.width = screen.width + "px";
	var bottom = document.getElementById("bottom");
	bottom.style.width = screen.width + "px";
	user.style.height = screen.height + "px";
	/*
	 * 设置轮播图位置高度大小
	 * 将高度设置成第一张图片的高度
	 */
	var containerImg = document.getElementsByClassName("containerImg")[0];
	containerImg.style.height = getStyle(imgArr[0],"height");
	for(var i = 1;i < imgArr.length;i++){
		imgArr[i].style.height = getStyle(imgArr[0],"height");
	}
	/*
	 * 设置轮播图导航栏位置
	 */
	var navDiv = document.getElementById("navDiv");
	navDiv.style.left = (screen.width - navDiv.offsetWidth) / 2 + "px";
	/*
	 * 点击头像，使用户页面弹出
	 */
	var all = document.getElementById("all");
	userImage.addEventListener("touchend",function() {
		//alert("点击了");
		move(user, "left", 0, 40);
		moveOpa(0.4,0.4);
		addClass(all,"position_fixed");
		function getCookie(cookieName) {
			var allcookies = document.cookie;
			var cookiePos = allcookies.indexOf(cookieName); //索引的长度
			if (cookiePos != -1) {
				// 把cookie_pos放在值的开始，只要给值加1即可。
				cookiePos += cookieName.length + 1;
				var cookieEnd = allcookies.indexOf(";", cookiePos);

				if (cookieEnd == -1) {
					cookieEnd = allcookies.length;
				}
				var value = decodeURI(allcookies.substring(cookiePos, cookieEnd));
			}
			else{}
			return value;
		}

		var name = getCookie("Name");
		var phone = getCookie("Phone");
		var number = getCookie("Number");
		var faculty = getCookie("Naculty");
		var identity = getCookie("Identity");
		var school = getCookie("School");
		var energy = getCookie("Energy");
		console.log(name);
		console.log(number);
		console.log(document.getElementById("username"));
		document.getElementById("username1").innerHTML = name;
		document.getElementById("number").innerHTML = number;
	},false);
/*
 * 当前用户页面弹出，点击空白地区，使用户页面收回
	 */
	back_page.onclick = function() {
		move(user, "left", -screen.width, 50);
		moveOpa(1.0,0.4);
		removeClass(all,"position_fixed");
	};
	var touchstartX,touchstartY;
	var t3,t4;
	back_page.addEventListener('touchstart',function(event){
		t3 = new Date().getTime();
		touchstartX = event.touches[0].pageX;
		touchstartY = event.touches[0].pageY;
	},false);
	back_page.addEventListener('touchmove',function(){
		var touchmoveX = event.touches[0].pageX - touchstartX;
		if(touchmoveX >= 0){
			touchmoveX = 0;
		}
		user.style.left = touchmoveX + "px"; 
	},false);
	back_page.addEventListener('touchend',function(event){
		t4 = new Date().getTime();
		t3 = t4 - t3;
		var endX, endY;
		endX = event.changedTouches[0].pageX;
		endY = event.changedTouches[0].pageY;
		var direction = GetSlideDirection(touchstartX,touchstartY,endX,endY);

		/*
			如果滑动快，方向对则直接复原
			如果滑动慢，则等end时判断方向，对则复原
		*/
		if(t3 < 666){
			if(direction == 3){
				move(user, "left", -screen.width, 50,function(){
					moveOpa(1.0,0.4);
					removeClass(all,"position_fixed");
				});
			}
		}else{
			var moveX = Math.abs(endX - touchstartX);
				
			if(moveX >= (screen.width/3)){
				//手指移动距离，切换
				move(user, "left", -screen.width, 50,function(){
					moveOpa(1.0,0.4);
					removeClass(all,"position_fixed");
				});
			}
		}
	},false);
	// /*
	//  * 轮播图
	//  */
	//获取所有的a
	var allA = navDiv.getElementsByTagName("a");
	/*
	 * 为a设置变色，将a与图片链接起来
	 */
	allA[0].style.opacity = 1;
	function setA(){
		if(index >= imgArr.length - 2){
			//目前到最后一张了，直接切换到第二张（第一张）
			index = 0;
			imgList.style.left = -screen.width + "px";
		}
		if(index <= -1){
			//目前使第一张(第三张)，直接切换到第四张（第五张）
			index = 2;
			imgList.style.left = -screen.width*3 + "px";
			
		}
		/*
		 * 遍历所有的a,设置opacity
		 */
		for(var i = 0;i < allA.length; i++){
			allA[i].style.opacity = 0.3;
		}
		/*
		 * 将目前所处的a设置
		 */
		allA[index].style.opacity = 1;
		
	}
	//图片索引
	var index = 0;
	//3 1 2 3 1
	var newleft;
	function next_pic(){
		if(parseInt(getStyle(imgList,"left")) == -screen.width*(imgArr.length - 1)){
			
			newleft = -2*screen.width;
		}else{
			newleft = parseInt(getStyle(imgList,"left")) - screen.width;
		}
		imgList.style.left = newleft + "px";
		
	}
	function prev_pic(){
		if(parseInt(getStyle(imgList,"left")) == 0){
			newleft = - (imgArr.length - 3)*screen.width;
		}else{
			newleft = parseInt(imgList.style.left) + screen.width;
		}
		imgList.style.left = newleft + "px";


	}
	var timer;
	function autoChange(){
		timer = setInterval(function(){
			index++;
			index %= (imgArr.length-2);
			next_pic();
			setA();
		},3000);
	}
	autoChange();
	
	
	
/*轮播滑动操作 */
		var body = document.body;
		//滑动处理
		var startX, startY;
		var imgList_L;
		var t1,t2;
		containerImg.addEventListener('touchstart', function(ev) {
			t1 = new Date().getTime();
			/*
			 * 关闭轮播，添加类名，阻止页面上下滑动
			 */
			clearInterval(timer);
			addClass(body,"position_fixed");
			
			//获取最开始的手指落点坐标
			startX = ev.touches[0].pageX;
			startY = ev.touches[0].pageY;
			//
			imgList_L = parseInt(getStyle(imgList,"left"));
			
		}, false);
		var moveX;
		containerImg.addEventListener("touchmove", function(event) {
			/*
			 * 向右为正
			 */
			moveX = imgList_L + event.touches[0].pageX - startX;
			//console.log(event.touches[0].pageX - startX + "，"+getStyle(imgList,"left"));
			imgList.style.left = moveX + "px";
			
		})
		containerImg.addEventListener('touchend', function(ev) {
			t2 = new Date().getTime();
			t1 = t2 - t1;

						/*
			 * 判断是否过界
			 */
			var endX, endY;
			endX = ev.changedTouches[0].pageX;
			endY = ev.changedTouches[0].pageY;

			var direction = GetSlideDirection(startX, startY, endX, endY);
			function checkIndex(){
				if(index >= imgArr.length - 2){
					//目前到最后一张了，直接切换到第二张（第一张）
					index = 0;
					imgList.style.left = -screen.width + "px";
				}
				if(index <= -1){
					//目前使第一张(第三张)，直接切换到第四张（第五张）
					index = 2;
					imgList.style.left = -screen.width*3 + "px";
					
				}
			}
			/*
			 * 判断方向进行移动设置a
			 */
			function checkDirection(){
				
				//判断移动方向
				if(direction == 3){
					//向左滑动
					//判断目标位置是否过界
					checkIndex();
					index++;
					move(imgList,"left",-screen.width*(index+1),30,function(){
						setA();
					});
				}else if(direction == 4){
					checkIndex();
					index--;
					move(imgList,"left",-screen.width*(index+1),30,function(){
						setA();
					});
				}
			}
			/*
			 * 移动时间短 -> 获取方向切换
			 * 移动时间长 -> 计算手指移动距离 -> 过半则获取方向切换，没有过半则恢复
			 */
			var imgList = document.getElementById("imgList");
			if(t1 < 666){
			//console.log("拖动时间短");

				checkDirection();
			}else{
				//时间过长，计算手指移动距离，距离过半判断方向开始进行切换
				//距离不过半，恢复原位
				//0 1 2
				var moveX = Math.abs(endX - startX);
				
				if(moveX >= (screen.width/2)){
					//手指移动距离过半，切换
					checkDirection();
				}
			}

			/*
			 * 手指抬起，开启轮播效果，移除类名，页面可以上下滑动
			 */
			
			autoChange();
			removeClass(body,"position_fixed");
	

		}, false);




		function createMission(personSend,missionTime,missionName,missionContent,energy,personAccept,personNumberA,personNumberS,check,price,place,missionId){
			var missions = document.getElementById("missions");
			var missionNew = document.createElement("div");
			var br = document.createElement('br');
			missionNew.className = "missionForm";
			missions.appendChild(missionNew);
			//开始添加form里边的东西
			myAppendChildClass(missionNew,"div","avatar","");
			myAppendChildClass(missionNew,"p","personNumberS",personNumberS);
			myAppendChildClass(missionNew,"p","personSend",personSend);
			myAppendChildClass(missionNew,"p","missionTime",missionTime);
			myAppendChildClass(missionNew,"p","missionMon","悬赏值:<span class='price'>"+energy+"</span>");
			myAppendChildClass(missionNew,"p","missionName",missionName);
			myAppendChildClass(missionNew,"p","missionContent",missionContent);
			myAppendChildClass(missionNew,"p","personNumberA",personNumberA);
			myAppendChildClass(missionNew,"p","personAccept",personAccept);
			myAppendChildClass(missionNew,"p","check",check);
			myAppendChildClass(missionNew,"p","place",place);
			var accept=myAppendChildClass(missionNew,"button","accept","接受");
			var think=myAppendChildClass(missionNew,"button","think","再想想/放弃");
			accept.setAttribute("id",missionId);
		}
		//这个不存cookie
		// searchGuangChangIndex();
		// var missionList=getCookie("selectResults");
		// if(missionList.trim()=="error"){
		// 	alert("missionLIst 后端崩溃");
		// }
		// missionList=JSON.parse(missionList);
		// var recipient;
		// for(var i=0;i<missionList.length;i++){
		// 	if(missionList[i].recipient.trim()==""){
		// 		recipient="false";
		// 	}
		// 	else{
		// 		recipient="true";
		// 	}
		// 	createMission(missionList[i].author,missionList[i].createTime,missionList[i].title,missionList[i].content,missionList[i].reward,"接受者",missionList[i].recipient,"发送者学号:"+missionList[i].userid,recipient,missionList[i].reward,missionList[i].place);
		// }
		//自动给父节点添加子节点并加上class名字
		function myAppendChildClass(father,sonType,className,innerContent){
			var son = document.createElement(sonType);
			son.className = className;
			son.innerHTML = innerContent;
			father.appendChild(son);
		}


		/*弹出窗口*/
		var missions = document.getElementById("missions");
		var missionForm = document.getElementsByClassName("missionForm");
		var elasticFrame = document.getElementById("elasticFrame");
		
		var closeBtn = document.getElementById("closeBtn");
		var show = document.getElementById("mission");
		var showP = elasticFrame.getElementsByTagName("p");
		// for(var i = 0;i < missionForm.length;i++){
		// 	missionBind(missionForm[i],show,elasticFrame);
		// }
		var show = document.getElementById("mission");
		closeBtn.onclick = function(){
			elasticFrame.style.display = "none";
			document.body.style.overflow = "";
			show.removeChild(show.lastChild);
		}
}

